﻿using System;
using System.Data;
using System.Data.Common;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.Helper;

namespace YiYiTong.Rocket.DataAccess
{
    public class ShoppingCartDataAccess
    {
        #region 单例模式
        private ShoppingCartDataAccess()
        {
        }

        private static ShoppingCartDataAccess _instance;
        private static readonly object _syncObject = new object();

        public static ShoppingCartDataAccess Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncObject)
                    {
                        if (_instance == null)
                        {
                            _instance = new ShoppingCartDataAccess();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion 单例模式

        /// <summary>
        /// 商品加入购物车
        /// </summary>
        public int Add(long uid, long goods_id, out string stroutput)
        {
            int ret = 0;
            stroutput = "商品加入购物车失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_shoppingcart_add";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@goods_id", DbType.Int64, goods_id);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int32, 8);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (int)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("商品加入购物车-ShoppingCartAdd" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 修改购物车商品数量
        /// </summary>
        public int Edit(long uid, long goods_id, int num, out string stroutput)
        {
            int ret = 0;
            stroutput = "修改商品数量失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_shoppingcart_edit";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@goods_id", DbType.Int64, goods_id);
                db.AddInParameter(Procdbcomm, "@num", DbType.Int32, num);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int32, 8);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (int)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("修改商品数量失败-ShoppingCartEdit" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 删除购物车商品
        /// </summary>
        /// <param name="seq_id"></param>
        /// <returns></returns>
        public int Del(long seq_id)
        {
            int ret = 0;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sqlName = "delete from ADZ_ShoppingCart where seq_id=@seq_id";
                DbCommand Procdbcomm = db.GetSqlStringCommand(sqlName);
                db.AddInParameter(Procdbcomm, "@seq_id", DbType.Int64, seq_id);
                ret = db.ExecuteNonQuery(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("删除购物车商品-ShoppingCartDel" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 获取购物车商品
        /// 
        /// </summary>
        /// <returns></returns>
        public DataTable GetAll(long uid)
        {
            DataTable dt = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sqlName = @"proc_shoppingcart_getall";
                DbCommand Procdbcomm = db.GetStoredProcCommand(sqlName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                var ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    dt = ds.Tables[0];
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取购物车商品-ShoppingCartGetAll" + ex.ToString());
            }
            return dt;
        }

        /// <summary>
        /// 获取购物车单个商品
        /// </summary>
        /// <returns></returns>
        public DataTable GetInfo(long uid, long seq_id)
        {
            DataTable dt = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sqlName = @"SELECT A.seq_id,a.uid,A.amount AS buy_amount,A.total_money AS  buy_total_money, B.* 
                                   FROM ADZ_ShoppingCart A 
                                   LEFT JOIN Views_Goods B ON A.goods_id=B.goods_id
                                   WHERE [uid]=@uid and seq_id=@seq_id";
                DbCommand Procdbcomm = db.GetSqlStringCommand(sqlName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@seq_id", DbType.Int64, seq_id);
                var ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    dt = ds.Tables[0];
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取购物车单个商品-ShoppingCartGetInfo" + ex.ToString());
            }
            return dt;
        }
    }
}
